#include <QMessageBox>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include "newRate.h"
#include "MyLineEdit.h"
#include "ResourceDef.h"
#include "ResourceController.h"
#include "ResourceBase.h"

newRate::newRate(QWidget *parent)
    : QWidget(parent)
    , m_mainLyt(this)
    , m_inputlyt()
    , m_incomingLines()
    , m_outgoingLines()
{
    QLabel* title = new QLabel(QString::fromUtf8("本计算器适用于2011年9月1日新税法"));
    title->setAlignment(Qt::AlignCenter);
    m_mainLyt.addWidget(title);
    m_mainLyt.addLayout(&m_inputlyt);
    initIncomingItem();
    initOutgoingItem();
    initButtons();
}

newRate::~newRate()
{
}

void newRate::initIncomingItem()
{
    QGroupBox* grpBox = new QGroupBox(QString::fromUtf8("收入项"));
    QHBoxLayout* boxlyt = new QHBoxLayout(grpBox);
    QVBoxLayout* hboxlytForLabel = new QVBoxLayout();
    QVBoxLayout* hboxlytForLine = new QVBoxLayout();

    QMap<IncomeItem, QString> incomingMap = ResourceController::inst()->getIncomingItem();
    QMapIterator<IncomeItem, QString> i(incomingMap);
    while(i.hasNext()) {
        i.next();
        QLabel* lbl = new QLabel(i.value());
        MyLineEdit* line = new MyLineEdit();
        connect(line, SIGNAL(valueChanged(QString,QLineEdit*)), this, SLOT(onLineEditValueChanged(QString,QLineEdit*)));
        m_incomingLines.append(line);
        hboxlytForLabel->addWidget(lbl);
        hboxlytForLine->addWidget(line);
    }
    boxlyt->addLayout(hboxlytForLabel);
    boxlyt->addLayout(hboxlytForLine);
    m_inputlyt.addWidget(grpBox);
}

void newRate::initOutgoingItem()
{
    QGroupBox* grpBox = new QGroupBox(QString::fromUtf8("扣除项"));
    QHBoxLayout* boxlyt = new QHBoxLayout(grpBox);
    QVBoxLayout* hboxlytForLabel = new QVBoxLayout();
    QVBoxLayout* hboxlytForLine = new QVBoxLayout();

    QMap<OutgointItem, QString> outgoingMap = ResourceController::inst()->getOutgointItem();
    QMapIterator<OutgointItem, QString> i(outgoingMap);
    while(i.hasNext()) {
        i.next();
        QLabel* lbl = new QLabel(i.value());
        MyLineEdit* line = new MyLineEdit();
        connect(line, SIGNAL(valueChanged(QString,QLineEdit*)), this, SLOT(onLineEditValueChanged(QString,QLineEdit*)));
        m_outgoingLines.append(line);
        hboxlytForLabel->addWidget(lbl);
        hboxlytForLine->addWidget(line);
    }
    boxlyt->addLayout(hboxlytForLabel);
    boxlyt->addLayout(hboxlytForLine);
    m_inputlyt.addWidget(grpBox);
}

void newRate::initButtons()
{
    QHBoxLayout* hboxlyt = new QHBoxLayout();
    QPushButton* btnCaculate = new QPushButton(QString::fromUtf8("计算"));
    QPushButton* btnReset = new QPushButton(QString::fromUtf8("重置"));
    connect(btnCaculate, SIGNAL(clicked()), this, SLOT(on_btnCalculate_clicked()));
    connect(btnReset, SIGNAL(clicked()), this, SLOT(on_btnReset_clicked()));
    hboxlyt->addWidget(btnCaculate);
    hboxlyt->addWidget(btnReset);
    m_mainLyt.addLayout(hboxlyt);
}

void newRate::on_btnReset_clicked()
{
    foreach (QLineEdit * line, m_incomingLines) {
        line->clear();
    }

    foreach (QLineEdit * line, m_outgoingLines) {
        line->clear();
    }
}

void newRate::on_btnCalculate_clicked()
{
    bool ok1 = false;
    bool ok2 = false;
    float startMoney = ResourceController::inst()->getStartMoney();
    float incomingPay = 0.0f;
    foreach (QLineEdit * line, m_incomingLines) {
        incomingPay += line->text().toFloat(&ok1);
    }

    float outgoingPay = 0.0f;
    foreach (QLineEdit * line, m_outgoingLines) {
        outgoingPay += line->text().toFloat(&ok2);
    }

    float moneyTmp = incomingPay - outgoingPay;
    if (moneyTmp <= 0) {
        QMessageBox::critical(this, QString::fromUtf8("输入有误"), QString::fromUtf8("请重新输入!"));

        return;
    }
    moneyTmp -= startMoney;
    if (0 <= moneyTmp) {
        RateTable rateTbl= ResourceController::inst()->calculateRate(moneyTmp);
        moneyTmp = moneyTmp * rateTbl.ratePersent - rateTbl.deletenum;
        QMessageBox::information(this, QString::fromUtf8("所需要缴纳的个人所得税"), QString::fromUtf8("您所需缴纳的个人所得税为: %1 元").arg(moneyTmp));
    }
    else {
        QMessageBox::information(this, QString::fromUtf8("无需缴纳税款"), QString::fromUtf8("由于减去扣除项后余额小于%1，所以您无需缴纳个人所得税！").arg(startMoney));
    }
}

void newRate::onLineEditValueChanged(QString arg1, QLineEdit *line)
{
    if ((!arg1.left(arg1.length() - 1).contains("."))
            &&(0 == arg1.right(1).compare("."))) {
        return;
    }

    bool ok = false;
    arg1.right(1).toInt(&ok, 10);
    if (!ok) {
        QString ss;
        ss = arg1.left(arg1.length() - 1);
        line->setText(ss);
        line->setFocus();
    }
}
